/*
 * DownloadAddBox.java
 *
 * Created on 17 de Fevereiro de 2008, 18:13
 */
package downloadmanager;

import java.net.MalformedURLException;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author  MX
 */
public class DownloadAddBox extends javax.swing.JDialog {

    /** Creates new form DownloadAddBox */
    public DownloadAddBox(java.awt.Frame parent) {
        super(parent);
        initComponents();
    }

    @Action
    public void closeDownloadAddBox() {
        setVisible(false);
        textURL.setText("");
    }

    @Action
    public void doDownloadAdd() {
        try
        {
            Download f = App.getApplication().getDownloadsModel().create(textURL.getText());

            JFrame mainFrame = App.getApplication().getMainFrame();
            JDialog addBox = new DownloadAddBox2(mainFrame, f);
            addBox.setLocationRelativeTo(mainFrame);
            App.getApplication().show(addBox);
            closeDownloadAddBox();
        } catch (MalformedURLException e) {
            JOptionPane.showMessageDialog(this, "URL introduzido inválido. Apenas suporta o seguinte protocolo: http", "URL Inválido", JOptionPane.WARNING_MESSAGE);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        textURL = new javax.swing.JTextField();
        buttonCancel = new javax.swing.JButton();
        buttonOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(downloadmanager.App.class).getContext().getResourceMap(DownloadAddBox.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setForeground(resourceMap.getColor("Form.foreground")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        textURL.setText(resourceMap.getString("textURL.text")); // NOI18N
        textURL.setName("textURL"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(downloadmanager.App.class).getContext().getActionMap(DownloadAddBox.class, this);
        buttonCancel.setAction(actionMap.get("closeDownloadAddBox")); // NOI18N
        buttonCancel.setName("buttonCancel"); // NOI18N

        buttonOK.setAction(actionMap.get("doDownloadAdd")); // NOI18N
        buttonOK.setText(resourceMap.getString("buttonOK.text")); // NOI18N
        buttonOK.setName("buttonOK"); // NOI18N
        buttonOK.setPreferredSize(new java.awt.Dimension(75, 23));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textURL, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(buttonOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textURL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField textURL;
    // End of variables declaration//GEN-END:variables
}